//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include "CVariableOfAStringBuf.h"
#include "CObjInfoList.h"

UInt32 CVariableOfAStringBuf::AddRef()
{
    Int32 nRef = m_cRef.Increment();
    return (UInt32)nRef;
}

UInt32 CVariableOfAStringBuf::Release()
{
    Int32 nRef = m_cRef.Decrement();

    if (0 == nRef) {
        delete this;
    }

    return nRef;
}

PInterface CVariableOfAStringBuf::Probe(REIID riid)
{
    if (riid == EIID_IInterface) {
        return (PInterface)(IVariableOfAStringBuf *)this;
    }
    else if (riid == EIID_IVariableOfAStringBuf) {
        return (IVariableOfAStringBuf *)this;
    }
    else if (riid == EIID_IAStringBufSetter) {
        return (IAStringBufSetter *)this;
    }
    else if (riid == EIID_IAStringBufGetter) {
        return (IAStringBufGetter *)this;
    }
    else {
        return NULL;
    }
}

ECode CVariableOfAStringBuf::Aggregate(AggregateType type, PInterface pObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CVariableOfAStringBuf::GetDomain(PInterface *ppObj)
{
    return E_NOT_IMPLEMENTED;
}

CVariableOfAStringBuf::CVariableOfAStringBuf(
    /* [in] */ PAStringBuf pAsb,
    /* [in] */ Boolean bAlloc)
{
    m_pAsb = pAsb;
    m_bAlloc = bAlloc;
}

CVariableOfAStringBuf::~CVariableOfAStringBuf()
{
    if (m_bAlloc && m_pAsb) {
        AStringBuf::Free(m_pAsb);
    }
}

ECode CVariableOfAStringBuf::GetTypeInfo(
    /* [out] */ IDataTypeInfo ** ppTypeInfo)
{
    return g_objInfoList.AcquireIntrinsicInfo(CarDataType_WChar, ppTypeInfo);
}

ECode CVariableOfAStringBuf::GetPayload(
    /* [out] */ PVoid * pPayload)
{
    if (!pPayload) {
        return E_INVALID_ARGUMENT;
    }

    *pPayload = m_pAsb;
    return NOERROR;
}

ECode CVariableOfAStringBuf::Rebox(
    /* [in] */ PVoid localVariablePtr)
{
    PAStringBuf pAsb = (PAStringBuf)localVariablePtr;
    if (!pAsb || !(pAsb->m_flags & CarQuintetFlag_Type_WChar)
        || !pAsb->GetCapacity()) {
        return E_INVALID_ARGUMENT;
    }

    if (m_bAlloc && m_pAsb) {
        AStringBuf::Free(m_pAsb);
        m_bAlloc = FALSE;
    }

    m_pAsb = pAsb;

    return NOERROR;
}

ECode CVariableOfAStringBuf::GetSetter(
    /* [out] */ IAStringBufSetter ** ppSetter)
{
    if (!ppSetter) {
        return E_INVALID_ARGUMENT;
    }

    this->AddRef();
    *ppSetter = (IAStringBufSetter *)this;
    return NOERROR;
}

ECode CVariableOfAStringBuf::GetGetter(
    /* [out] */ IAStringBufGetter ** ppGetter)
{
    if (!ppGetter) {
        return E_INVALID_ARGUMENT;
    }

    this->AddRef();
    *ppGetter = (IAStringBufGetter *)this;
    return NOERROR;
}

ECode CVariableOfAStringBuf::SetElement(
    /* [in] */ Int32 index,
    /* [in] */ AChar character)
{
    if (index < 0 || index >= m_pAsb->GetLength()) {
        return E_INVALID_ARGUMENT;
    }

    (*m_pAsb)[index] = character;
    return NOERROR;
}

ECode CVariableOfAStringBuf::SetData(
    /* [in] */ AString str)
{
    if (str.IsNull()) {
        return E_INVALID_ARGUMENT;
    }

    m_pAsb->Copy(str);
    return NOERROR;
}

ECode CVariableOfAStringBuf::GetCapacity(
    /* [out] */ Int32 * pCapacity)
{
    if (!pCapacity) {
        return E_INVALID_ARGUMENT;
    }

    *pCapacity = m_pAsb->GetCapacity();
    return NOERROR;
}

ECode CVariableOfAStringBuf::GetLength(
    /* [out] */ Int32 * pLength)
{
    if (!pLength) {
        return E_INVALID_ARGUMENT;
    }

    *pLength = m_pAsb->GetLength();
    return NOERROR;
}

ECode CVariableOfAStringBuf::GetElement(
    /* [in] */ Int32 index,
    /* [out] */ AChar * character)
{
    if (index >= m_pAsb->GetLength()) {
        return E_INVALID_ARGUMENT;
    }

    *character = (*m_pAsb)[index];
    return NOERROR;
}

ECode CVariableOfAStringBuf::GetData(
    /* [out] */ AStringBuf * stringBuf)
{
    if (!stringBuf) {
        return E_INVALID_ARGUMENT;
    }

    stringBuf->Copy(m_pAsb->GetPayload());

    return NOERROR;
}
